import { MessageDto } from "../../../structures/dto/Message";
import { Payload } from "../../../structures/dto/Payload";
import { Client } from "../../Client";

/*
    {
        "t":"MESSAGE_CREATE",
        "s":2,
        "op":0,
        "d":{
            "type":0,
            "tts":false,
            "timestamp":"2023-11-18T11:40:34.025000+00:00",
            "referenced_message":null,
            "pinned":false,
            "nonce":"1175400147471826944",
            "mentions":[],
            "mention_roles":[],
            "mention_everyone":false,
            "member":{
                "roles":[
                    "172440752045948928",
                    "273144052961247232",
                    "309696385563885570",
                    "427902078912954368",
                    "402021459440173056",
                    "585537733229936640",
                    "176055079445463050",
                    "248836314655358986",
                    "507260546119368714"
                ],
                "premium_since":"2019-06-04T18:42:13.573000+00:00",
                "pending":false,
                "nick":null,
                "mute":false,
                "joined_at":"2016-04-20T23:47:27.029000+00:00",
                "flags":2,
                "deaf":false,
                "communication_disabled_until":"2022-01-10T22:24:07.867000+00:00",
                "avatar":null
            },
            "id":"1175400151620263996",
            "flags":0,
            "embeds":[],
            "edited_timestamp":null,
            "content":"just sying errors isnt very useful",
            "components":[],
            "channel_id":"1014990256493314169",
            "author":{
                "username":"spaten",
                "public_flags":0,
                "premium_type":2,
                "id":"119223402920673280",
                "global_name":"Spaten",
                "discriminator":"0",
                "avatar_decoration_data":null,
                "avatar":"7b5b8296655458b6b2c35220d2b49ff5"
            },
            "attachments":[],
            "guild_id":"172440238717665280"
        }
    }


    {
        "type":0,
        "tts":false,
        "timestamp":"2023-11-19T17:46:03.566000+00:00",
        "referenced_message":null,
        "pinned":false,
        "nonce":"1175854501597282304",
        "mentions":[],
        "mention_roles":[],
        "mention_everyone":false,
        "member":{
            "roles":[
                "273144052961247232",
                "402021459440173056",
                "176054693108252682"
            ],
            "premium_since":null,
            "pending":false,
            "nick":null,
            "mute":false,
            "joined_at":"2021-04-15T16:08:20.202000+00:00",
            "flags":2,
            "deaf":false,
            "communication_disabled_until":null,
            "avatar":null
        },
        "id":"1175854518647394405",
        "flags":0,
        "embeds":[],
        "edited_timestamp":null,
        "content":"",
        "components":[],
        "channel_id":"215496276609990656",
        "author":{
            "username":"phoenix7700",
            "public_flags":0,
            "premium_type":0,
            "id":"111280187584348160",
            "global_name":"phoenix7700",
            "discriminator":"0",
            "avatar_decoration_data":null,
            "avatar":"9083ea5f9714a6a0bcb9471fd30196e5"
        },
        "attachments":[
            {
                "width":233,
                "url":"https://cdn.discordapp.com/attachments/215496276609990656/1175854518324441199/img.png",
                "size":2672,
                "proxy_url":"https://media.discordapp.net/attachments/215496276609990656/1175854518324441199/img.png",
                "placeholder_version":1,
                "placeholder":"kucFCYRWaHd5dweY2HgPbOY=",
                "id":"1175854518324441199",
                "height":17,
                "filename":"image.png",
                "content_type":"image/png",
                "content_scan_version":1
            }
        ],
        "guild_id":"172440238717665280"
    }


    {
        "t": "MESSAGE_CREATE",
        "s": 3,
        "op": 0,
        "d": {
            "type": 0,
            "tts": false,
            "timestamp": "2023-12-08T21:50:48.448000+00:00",
            "referenced_message": null,
            "pinned": false,
            "nonce": "1182801473696694272",
            "mentions": [],
            "mention_roles": [],
            "mention_everyone": false,
            "id": "1182801480953114674",
            "flags": 0,
            "embeds": [
                {
                    "video": {
                        "width": 640,
                        "url": "https://media.tenor.com/PB68qd-83XEAAAPo/omelette-dexter-laboratory.mp4",
                        "proxy_url": "https://images-ext-1.discordapp.net/external/FwbLx15VyQoMQeLk8q403Zphy5zNiLIOlwqUSBEFQ9Y/https/media.tenor.com/PB68qd-83XEAAAPo/omelette-dexter-laboratory.mp4",
                        "height": 478
                    },
                    "url": "https://tenor.com/view/omelette-dexter-laboratory-whisper-gif-15038986",
                    "type": "gifv",
                    "thumbnail": {
                        "width": 640,
                        "url": "https://media.tenor.com/PB68qd-83XEAAAAe/omelette-dexter-laboratory.png",
                        "proxy_url": "https://images-ext-1.discordapp.net/external/NSGxFWPA6hX7HxO1IOeF337sM_ioX8lu5Y504U24kJE/https/media.tenor.com/PB68qd-83XEAAAAe/omelette-dexter-laboratory.png",
                        "height": 478
                    },
                    "provider": {
                        "url": "https://tenor.co",
                        "name": "Tenor"
                    }
                }
            ],
            "edited_timestamp": null,
            "content": "https://tenor.com/view/omelette-dexter-laboratory-whisper-gif-15038986",
            "components": [],
            "channel_id": "1176621666923712669",
            "author": {
                "username": "timosh",
                "public_flags": 0,
                "premium_type": 0,
                "id": "118483467766857735",
                "global_name": "Timosh",
                "discriminator": "0",
                "avatar_decoration_data": null,
                "avatar": "643fbd518376a074858bcf41a1c36751"
            },
            "attachments": []
        }
    }

    {
        "t": "MESSAGE_CREATE",
        "s": 21,
        "op": 0,
        "d": {
            "type": 0,
            "tts": false,
            "timestamp": "2023-12-08T21:52:17.055000+00:00",
            "referenced_message": null,
            "pinned": false,
            "nonce": "1182801845924397056",
            "mentions": [],
            "mention_roles": [],
            "mention_everyone": false,
            "id": "1182801852597797005",
            "flags": 0,
            "embeds": [],
            "edited_timestamp": null,
            "content": "https://tenor.com/view/angry-cat-triggered-ahhh-meeeeoow-gif-16764869",
            "components": [],
            "channel_id": "1176621666923712669",
            "author": {
                "username": "timosh",
                "public_flags": 0,
                "premium_type": 0,
                "id": "118483467766857735",
                "global_name": "Timosh",
                "discriminator": "0",
                "avatar_decoration_data": null,
                "avatar": "643fbd518376a074858bcf41a1c36751"
            },
            "attachments": []
        }
    }
*/

export function MESSAGE_CREATE(client: Client, payload: Payload<MessageDto>) {

    const { d: data } = payload

    // console.log("MESSAGE_CREATE " + JSON.stringify(payload))
    
    if (!data) return;
    
    let prev_last_msg: string = ""

    if (client.guildChannels[data.channel_id]) {
        prev_last_msg = client.guildChannels[data.channel_id].last_message_id
        client.guildChannels[data.channel_id].last_message_id  = data.id
    } else if (client.privateChannels[data.channel_id]) {
        prev_last_msg = client.privateChannels[data.channel_id].last_message_id
        client.privateChannels[data.channel_id].last_message_id  = data.id
    }


    client.messageStore.addMessage(data.channel_id, [data])

    client.emit("message-new", data.channel_id, data.id, prev_last_msg)
}